package com.rightsidetech.http.controller.admin.activity;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.activity.dto.request.gift.*;
import com.rightsidetech.activity.dto.response.gift.*;
import com.rightsidetech.activity.service.ActGiftService;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.mall.dto.response.gift.ActGiftCommodityResp;
import com.rightsidetech.mall.entity.MallGiftCommodity;
import com.rightsidetech.mall.service.MallGiftCommodityService;
import com.rightsidetech.system.service.SysStoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/gift_act/")
@ShenyuSpringMvcClient(path = "/admin/gift_act/**")
@Api(tags = "换购活动相关")
public class ActGiftController extends BaseController {

    @Resource
    private ActGiftService actGiftService;
    @Resource
    private MallGiftCommodityService mallGiftCommodityService;
    @Resource
    private SysStoreInfoService sysStoreInfoService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/save")
    @ApiOperation("添加换购活动")
    public ResponseData save(@RequestBody ActGiftSaveReq req) throws Exception {
        List<Long> oldStoreIds = req.getStoreIds();
        ResponseData<List<Long>> data = saveStoreId(req);
        if(data.getCodeId() != 1){
            return data;
        }
        req.setStoreIds(data.getResData());
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if (null == loginInfoId || null == loginName) {
            return ResponseData.fail("未找到登录人");
        }
        return actGiftService.saveActGift(req, loginName, loginInfoId,oldStoreIds);
    }

    private ResponseData<List<Long>> saveStoreId(ActGiftSaveReq req){
        if(CollUtil.isEmpty(req.getAgentIds()) && CollUtil.isEmpty(req.getAreaIds()) && CollUtil.isEmpty(req.getStoreIds())){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"大区代理商门店id不可都为空");
        }
        List<Long> storeIds = new ArrayList<>();
        if(CollUtil.isNotEmpty(req.getAreaIds())){
            storeIds.addAll(sysStoreInfoService.selectIdsByAreaIds(req.getAreaIds()));
        }
        if(CollUtil.isNotEmpty(req.getAgentIds())){
            storeIds.addAll(sysStoreInfoService.selectIdsByAgentIds(req.getAgentIds()));
        }
        if(CollUtil.isNotEmpty(req.getStoreIds())){
            storeIds.addAll(req.getStoreIds());
        }
        if(CollUtil.isEmpty(storeIds)){
            return ResponseData.fail(CodeIdEnum.ParameterMalformed.getValue(),"未找到对应的门店");
        }
        return ResponseData.success(storeIds);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("分页换购活动")
    public ResponseData<PageInfo<ActGiftPageResp>> selectPage(ActGiftPageReq req){
         return actGiftService.page(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/detail")
    @ApiOperation("换购活动详情")
    public ResponseData<ActGiftDetailResp> details(@RequestParam(value = "id",required = true) Long id){
        ResponseData<ActGiftDetailResp> details = actGiftService.details(id);
        if(details.getCodeId() != 1){
            return details;
        }
        ActGiftDetailResp data = details.getResData();
        List<ActGiftDetailTypeResp> giftMallType = data.getGiftMallType();
        Map<Long,ActGiftCommodityResp> map = new HashMap<>();
        for (ActGiftDetailTypeResp typeResp : giftMallType) {
            List<ActGiftDetailComResp> giftCommodityList = typeResp.getGiftCommodityIds();
            for (ActGiftDetailComResp comResp : giftCommodityList) {
                ActGiftCommodityResp commodityResp = map.get(comResp.getGiftCommodityId());
                if(ObjectUtil.isEmpty(commodityResp)){
                    commodityResp = mallGiftCommodityService.selectActGiftById(comResp.getGiftCommodityId());
                }
                if(ObjectUtil.isEmpty(commodityResp)){
                    continue;
                }
                comResp.setCommodityName(commodityResp.getCommodityName());
                comResp.setTypeName(commodityResp.getTypeName());
                comResp.setBrandName(commodityResp.getBrandName());
                comResp.setSpecs(commodityResp.getSpecs());
                comResp.setPrice(commodityResp.getPrice());
                List<Integer> longs = actGiftService.selectByGiftId(comResp.getGiftCommodityId());
                for (Integer aLong : longs) {
                    comResp.setNum(aLong);
                }
                map.put(comResp.getGiftCommodityId(),commodityResp);
            }
        }
        return details;
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/arraignment")
    @ApiOperation("提审活动")
    public ResponseData arraignmentAct(@RequestBody DelReq req) {
       return actGiftService.arraignmentAct(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/verify")
    @ApiOperation("审核活动")
    public ResponseData verify(@RequestBody ActVerifyReq req) throws Exception{
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if (null == loginInfoId || null == loginName) {
            return ResponseData.fail("未找到登录人");
        }
        return actGiftService.verifyAct(req,loginName,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("修改活动")
    public ResponseData update(@RequestBody ActGiftUpReq req) throws Exception {
        ResponseData<List<Long>> data = saveStoreId(req);
        if(data.getCodeId() != 1){
            return data;
        }
//        req.setStoreIds(data.getResData());
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if (null == loginInfoId || null == loginName) {
            return ResponseData.fail("未找到登录人");
        }
        return actGiftService.updateAct(req, loginName, loginInfoId,data.getResData());
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除活动")
    public ResponseData del(@RequestBody DelReq req){
        return actGiftService.delete(req);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/count_page")
    @ApiOperation("活动统计分页")
    public ResponseData<PageInfo<ActGiftCountPageResp>> countPage(ActGiftCountPageReq req){
         return actGiftService.countActPage(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/count_detail")
    @ApiOperation("活动统计详情")
    public ResponseData<List<ActGiftCountDetailResp>> countDetail(DelReq req){
        ResponseData<List<ActGiftCountDetailResp>> result = actGiftService.countDetail(req);
        if(result.getCodeId() == 1){
            List<ActGiftCountDetailResp> list = result.getResData();
            for (ActGiftCountDetailResp resp : list) {
                MallGiftCommodity commodity = mallGiftCommodityService.selectTypeIdByGiftId(resp.getGiftId());
                resp.setGiftName(commodity.getCommodityName());
                resp.setSpecs(commodity.getSpecs());
            }
        }
        return result;
    }
}
